//
//  UpdatePwdVC.m
//  test
//
//  Created by 朱圳 on 2018/2/28.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "UpdatePwdVC.h"

@interface UpdatePwdVC ()

@end

@implementation UpdatePwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.title = @"修改密码";
    [self.oldPwd setLimitTextLength:18];
    [self.qPwd setLimitTextLength:18];
    [self.Pwd setLimitTextLength:18];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 修改密码 type 1为学生端 2位研究生端 3为教务端

 */
- (IBAction)updatePwd:(id)sender {
    if ([self.oldPwd.text isEqualToString:@""]) {
        [Utility showFail:@"请输入旧密码"];
    }else{
        if (self.Pwd.text.length < 6) {
            [Utility showFail:@"新密码长度不低于6位"];
        }else{
            if (self.Pwd.text != self.qPwd.text) {
                [Utility showFail:@"两次密码不一致"];
            }else{
                if ([self.type isEqualToString:@"1"]) {
                    NSDictionary *dict = @{
                                           @"sid" : Sid,
                                           @"spass" : Spassword,
                                           @"oldpass" : self.oldPwd.text,
                                           @"newpass" :self.Pwd.text
                                           };
                    [[AFNetworkTool shareManager]postJSONWithUrl:Stu_Changepass parmas:dict successData:^(id json) {
                        [Utility showSuccess:json[@"message"]];
                        if ([json[@"message"] isEqualToString:@"success"]) {
                              NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
                            [userDefaults removeObjectForKey:@"spassword"];
                            [userDefaults setObject:json[@"password"] forKey:@"spassword"];
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                      
                    } failure:^(NSError *error) {
                        [Utility showFail:@"网络连接出错..."];
                        NSLog(@"error%@",error);
                    }];
                }else if ([self.type isEqualToString:@"2"]){
                    NSDictionary *dict = @{
                                           @"gid" : Gid,
                                           @"gpass" :Gpassword,
                                           @"oldpass" :  self.oldPwd.text,
                                           @"newpass" :self.Pwd.text
                                           };
                    [[AFNetworkTool shareManager]postJSONWithUrl:GR_Changepass parmas:dict successData:^(id json) {
                        [Utility showSuccess:json[@"message"]];
                        if ([json[@"message"] isEqualToString:@"success"]) {
                            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
                            [userDefaults removeObjectForKey:@"gpassword"];
                            [userDefaults setObject:json[@"password"] forKey:@"gpassword"];
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                    } failure:^(NSError *error) {
                        [Utility showFail:@"网络连接出错..."];
                        NSLog(@"error%@",error);
                    }];
                }else if ([self.type isEqualToString:@"3"]){
                    NSDictionary *dict = @{
                                           @"jwid" : JWid,
                                           @"jwpassword" : JWpassword,
                                           @"oldpass" : self.oldPwd.text,
                                           @"newpass" :self.Pwd.text
                                           };
                    [[AFNetworkTool shareManager]postJSONWithUrl:JW_Changepass parmas:dict successData:^(id json) {
                        [Utility showSuccess:json[@"message"]];
                        if ([json[@"message"] isEqualToString:@"success"]) {
                            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
                            [userDefaults removeObjectForKey:@"jwPassword"];
                            [userDefaults setObject:json[@"password"] forKey:@"jwPassword"];
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                    } failure:^(NSError *error) {
                        [Utility showFail:@"网络连接出错..."];
                        NSLog(@"error%@",error);
                    }];
                }else{
                    NSLog(@"zz");
                }
                
            }
        }
    }

}

@end
